package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.VoiceMessages;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.VoiceMessagesService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

@DubboService
public class VoiceMessagesImpl implements VoiceMessagesService {

    @Autowired
    private MongoTemplate mongoTemplate;



    //保存语音
    @Override
    public void SaveVoice(String voiceUrl ,Long voiceUserId) {
        //今日日期yyyy-MM-dd
        String created = DateUtil.formatDate(new Date());
        //剩余语音条数
        Integer rqmainingTimes;
        //查询今天发了多少条语音
        Query query = new Query(Criteria.where("userId").is(voiceUserId).and("created").is(created));
        Long count = mongoTemplate.count(query, VoiceMessages.class);
        Integer countInt = new Long(count).intValue();

        //判断今天的数量
        if (countInt==0){
            rqmainingTimes=10;
        }else{
            rqmainingTimes=10-countInt;
        }
        //数量到大上限时
        if (rqmainingTimes==0){
            System.out.println("今日消息发送了有10条了");
        }else {
            //封装成VoiceMessages对象
            VoiceMessages voiceMessages = new VoiceMessages();
            voiceMessages.setUserId(voiceUserId);
            voiceMessages.setCreated(created);
            voiceMessages.setSoundUrl(voiceUrl);
            voiceMessages.setRqmainingTimes(rqmainingTimes);
            //保存进mongodb
            mongoTemplate.save(voiceMessages);
        }
    }
    //接收语音消息
    @Override
    public VoiceMessages receptionVoice(Long userId) {
        //查询接收用户为空的语音消息
        Query query = new Query(Criteria.where("reVoiceUserId").is(null));
        List<VoiceMessages> voiceMessagess = mongoTemplate.find(query, VoiceMessages.class);
        if (CollUtil.isNotEmpty(voiceMessagess)) {
            VoiceMessages voiceMessages = RandomUtil.randomEle(voiceMessagess);

            //今日日期yyyy-MM-dd
            String created = DateUtil.formatDate(new Date());
            //查询统计接收用户为空的语音消息
            query =new Query(Criteria.where("reVoiceUserId").is(userId).and("created").is(created));
            long count = mongoTemplate.count(query, VoiceMessages.class);
            int countInt = new Long(count).intValue();

            //判断接收语音消息有没有上限
            Integer remainingTimes;
            if (countInt==0){
                remainingTimes=10;
            }else{
                remainingTimes=10-countInt;
            }
            voiceMessages.setRemainingTimes(remainingTimes);
            voiceMessages.setReVoiceUserId(userId);
            mongoTemplate.save(voiceMessages);

            return voiceMessages;
        }
        return null;

    }
}
